package github.ziwukun.jsonql.lang;

import java.util.LinkedList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import github.ziwukun.jsonql.utils.CollectionUtils;

class SelectItemBuilder {
	String prop;
	List<SelectItemBuilder> childs = new LinkedList<>();
	
	SelectItemBuilder(String prop){
		if(StrUtil.isBlank(prop)) {
			throw new JSONQLBuildException("SelectItemBuilder的prop属性不能为空");
		}
		this.prop = prop;
	}
	
	SelectItemBuilder addChilds(List<SelectItemBuilder> childs) {
		childs.forEach(item -> {
			boolean containSameName = CollectionUtils.contains(this.childs, s -> {
				return s.prop.equals(item.prop);
			});
			if(containSameName) {
				throw new RuntimeException("重复添加相同名称的属性" + item.prop);
			}
			this.childs.add(item);
		});
		
		return this;
	}
	
	SelectItem build() {
		List<SelectItem> selects = new LinkedList<>();
		for(SelectItemBuilder selectItem:childs) {
			selects.add(selectItem.build());
		}
		return new SelectItemImpl(prop, new SelectImpl(selects));
	}
}
